package com.city.helper;


import com.city.config.Status;
import com.city.domain.city.CityEvent;
import com.city.domain.pub.PubUser;
import com.city.domain.report.*;
import com.city.format.CityEventFormat;
import com.city.format.CtDoBanFormat;
import com.city.format.CtReportFormat;
import com.city.repository.report.CtUserRepository;
import com.city.service.city.CityCopnoteMaincontentService;
import com.city.service.city.CityCopnoteObjectService;
import com.city.service.report.CtBranchService;
import com.city.service.report.ReportService;
import com.city.service.report.UserService;
import com.city.domain.city.CityTown;
import com.city.format.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @deprecation: 格式化助手
 * @email: leichuqiao@126.com
 * @author: 雷楚桥
 * @date: 2018/7/26 12:39
 */
@Component
public class FormatHelper {

    @Resource
    private ReportService reportService;
    @Resource
    private UserService userService;
    @Resource
    private AuthHelper authHelper;
    @Autowired
    private CityCopnoteMaincontentService contentService;
    @Autowired
    private CityCopnoteObjectService objectService;
    @Autowired
    private CtBranchService ctBranchService;
    @Autowired
    private CtUserRepository ctUserRepository;

    private static ReportService reportS;
    private static UserService userS;
    private static AuthHelper authH;
    private static CityCopnoteMaincontentService contentS;
    private static CityCopnoteObjectService objectS;



    @PostConstruct
    public void init() {
        reportS = reportService;
        userS = userService;
        authH = authHelper;
        contentS = contentService;
        objectS = objectService;
    }


    /**
     * @Author 何平
     * @Description //TODO 格式化展示数据
     * @Date 10:12 2018/8/16
     * @Param [towns]
     * @return java.util.List<com.city.format.CityReportFormat>
     **/
    public static List<CityReportFormat> cityReportFormat(List<CityTown> towns){
        List<CityReportFormat> list = new ArrayList<>();
        for (CityTown town : towns) {
            CityReportFormat info = new CityReportFormat();
            info.setTownName(town.getTownName());
            info.setReportNum( reportS.findAllByTownId(town.getTownId()));
            list.add(info);
        }
        return list;
    }

    /**
     * @param ctReportList List<CtReport> 上报内容
     * @return ctReportFormatList
     * @deprecation 格式化上报内容
     * @author 雷楚桥
     * @date 2018/7/26 12:39
     */
    public List<CtReportFormat> ctReportFormat(List<CtReport> ctReportList) {
        List<CtReportFormat> ctReportFormatList = new ArrayList<>();
            ctReportList.forEach(ctReport -> ctReportFormatList.add(ctReportFormat(ctReport)));
        return ctReportFormatList;
    }


    /**
     * @param ctReport CtReport 上报内容
     * @return ctReportFormat
     * @deprecation 格式化一条上报内容
     * @author 雷楚桥
     * @date 2018/7/26 15:05
     */
    public CtReportFormat ctReportFormat(CtReport ctReport) {
        CtReportFormat ctReportFormat = new CtReportFormat();
        ctReportFormat.setCtReport(ctReport);
        if(ctReport.getCategoryId() != null){
            ctReportFormat.setCategoryName(reportS.findCategoryById(ctReport.getCategoryId()).getName());
        }

        if(ctReport.getTypeBig() != null){
            String name;
            try{
                name = reportS.findEventById(ctReport.getTypeBig()).getName();
                ctReportFormat.setTypeBigName(name);
            }catch (Exception e){
                ctReportFormat.setTypeBigName(null);
            }

        }

        if (ctReport.getTypeSmall() != null)
            ctReportFormat.setTypeSmallName(reportS.findEventById(ctReport.getTypeSmall()).getName());

        ctReportFormat.setTownName(reportS.findTownById(ctReport.getTownId()).getTownName());
        ctReportFormat.setVillageName(reportS.findVillageById(ctReport.getVillageId()).getVillageName());
        ctReportFormat.setScheduleName(Status.REPORT_SCHEDULE.get(ctReport.getSchedule()));

        ctReportFormat.setCreateTimeName(TimeStampHelper.stampToDate(ctReport.getCreateTime()));

        Integer branchId = ctReport.getBranchId();
        if (branchId != null)
            ctReportFormat.setBranchName(branchId == 0 ? "指挥中心" : reportS.findBranchById(branchId).getName());


        ctReportFormat.setDoBan(reportS.checkDoBan(ctReport.getId(),authH.thisUserId()) ? 1 : 0);

        //上报人
        CtUser user = ctUserRepository.findOne(ctReport.getReportId());
        if (user != null) {
            ctReportFormat.setReportName(user.getName());
        }



        //受理员
        if(ctReport.getHandleManId() != null) {
            user = ctUserRepository.findOne(ctReport.getHandleManId());
            ctReportFormat.setHandleManName(user.getName());
        }

        //检察员
        if(ctReport.getCheckManId() != null){
            user = ctUserRepository.findOne(ctReport.getCheckManId());
            if(user == null)
                ctReportFormat.setCheckManName(null);
            else
            ctReportFormat.setCheckManName(user.getName());
        }

        return ctReportFormat;
    }


    /**
     * @param copnoteList List<CtCopnote> 未格式化的民情日记
     * @return CityCopnoteFormatList
     * @deprecation 格式化民情日记
     * @author 雷楚桥
     * @date 2018/7/28 15:49
     */
    public List<CtCopnoteFormat> ctCopnoteFormat(List<CtCopnote> copnoteList) {
        List<CtCopnoteFormat> copnoteFormatList = new ArrayList<>();
        copnoteList.stream().forEach(copnote -> {
            copnoteFormatList.add(ctCopnoteFormat(copnote));
        });
        return copnoteFormatList;
    }

    public CtCopnoteFormat ctCopnoteFormat(CtCopnote copnote) {
        CtCopnoteFormat copnoteFormat = new CtCopnoteFormat();
        copnoteFormat.setCopnote(copnote);
        copnoteFormat.setCreateTimeName(TimeStampHelper.stampToDate(copnote.getCreateTime()));
        copnoteFormat.setTownName(reportS.findTownById(copnote.getTownId()).getTownName());
        copnoteFormat.setVillageName(reportS.findVillageById(copnote.getVillageId()).getVillageName());
        copnoteFormat.setScheduleName(Status.COPNOTE_SCHEDULE.get(copnote.getSchedule()));

        //上报人
        CtUser user = ctUserRepository.findOne(copnote.getReportId());
        copnoteFormat.setReportName(user.getName());

        //受理员
        if(copnote.getHandleManId() !=null){
            user = ctUserRepository.findOne(copnote.getHandleManId());
            if(user != null) copnoteFormat.setHandleManName(user.getName());
        }


        //主要内容和主要对象
        copnoteFormat.setMainContentName(contentS.findNameById(copnote.getMainContentId()));
        copnoteFormat.setMainObjectName(objectS.findNameById(copnote.getMainObjectId()));

        return copnoteFormat;
    }


    /**
     * @param cityEventList List<CityEvent> gis上报内容
     * @return CityEventFormatList
     * @deprecation 格式化基于gis上报
     * @author 雷楚桥
     * @date 2018/7/28 16:26
     */
    public List<CityEventFormat> cityEventFormat(List<CityEvent> cityEventList) {
        List<CityEventFormat> cityEventFormatList = new ArrayList<>();
        cityEventList.stream().forEach(cityEvent -> {
            cityEventFormatList.add(cityEventFormat(cityEvent));
        });
        return cityEventFormatList;
    }

    public CityEventFormat cityEventFormat(CityEvent cityEvent) {
        CityEventFormat cityEventFormat = new CityEventFormat();
        cityEventFormat.setEvent(cityEvent);
        cityEventFormat.setScheduleName(Status.REPORT_SCHEDULE.get(cityEvent.getSchedule()));
        cityEventFormat.setCreateTimeName(TimeStampHelper.stampToDate(cityEvent.getCreateTime()));
        return cityEventFormat;
    }
    
    /**
     * @deprecation 格式化督办记录
     * @param doBan CtDoBan 督办记录
     * @return CtDoBanFormat
     * @author 雷楚桥
     * @date 2018-08-14 9:59
     */
    public CtDoBanFormat ctDoBanFormat(CtDoBan doBan){
        CtDoBanFormat doBanFormat = new CtDoBanFormat();
        doBanFormat.setDoBan(doBan);
        doBanFormat.setCreateTimeName(TimeStampHelper.stampToDate(doBan.getCreateTime(),2));
        return doBanFormat;
    }
    public List<CtDoBanFormat> ctDoBanFormat(List<CtDoBan> doBanList){
        List<CtDoBanFormat> doBanFormatList = new ArrayList<>();
        doBanList.stream().forEach(ctDoBan -> doBanFormatList.add(ctDoBanFormat(ctDoBan)));
        return doBanFormatList;
    }

    
    /**
     * @deprecation 格式化上报加统计
     * @param rep CtReport 上报案件
     * @param count Integer 案件统计
     * @return CtReportFormat
     * @author 雷楚桥
     * @date 2018-08-21 10:00
     */
    public CtReportFormat ctReportFormatCount(CtReport rep, Integer count) {
        CtReportFormat ctReportFormat = ctReportFormat(rep);
        ctReportFormat.setDoBanCount(count);
        return ctReportFormat;
    }


    public CtCountFormat ctCountFormat(CtCount ctc){
        System.out.println("1111-------------------------------");
        System.out.println(ctc);
        CtBranch ctBranch = ctBranchService.findById(ctc.getBranchId());
        System.out.println(ctBranch);
        CtCountFormat ctCountFormat = new CtCountFormat();
        ctCountFormat.setId(ctc.getId());
        ctCountFormat.setBranchName(ctBranch.getName());
        ctCountFormat.setDocketNum(ctc.getDocketNum());
        ctCountFormat.setShouldHandleNum(ctc.getShouldHandleNum());
        ctCountFormat.setHandleNum(ctc.getHandleNum());
        ctCountFormat.setOnTimeHandleNum(ctc.getOnTimeHandleNum());
        ctCountFormat.setOutTimeHandleNum(ctc.getOutTimeHandleNum());
        ctCountFormat.setInTimeHandleNum(ctc.getInTimeHandleNum());
        ctCountFormat.setOutTimeNotHandleNum(ctc.getOutTimeNotHandleNum());
        ctCountFormat.setReWorkNum(ctc.getReWorkNum());
        ctCountFormat.setReWorkRate(ctc.getReWorkRate());
        ctCountFormat.setShouldClosedNum(ctc.getShouldClosedNum());
        ctCountFormat.setRealClosedNum(ctc.getRealClosedNum());
        ctCountFormat.setRealClosedRate(ctc.getRealClosedRate());
        ctCountFormat.setOnTimeClosedNum(ctc.getOnTimeClosedNum());
        ctCountFormat.setOnTimeClosedRate(ctc.getOnTimeClosedRate());
        ctCountFormat.setOverallMerit(ctc.getOverallMerit());
        ctCountFormat.setRank(ctc.getRank());
        return ctCountFormat;

    }

    public CtCountFormat ctCountFormat(CtCount ctc,String name){
        CtCountFormat ctCountFormat = new CtCountFormat();
        ctCountFormat.setId(ctc.getId());
        ctCountFormat.setBranchName(name);
        ctCountFormat.setDocketNum(ctc.getDocketNum());
        ctCountFormat.setShouldHandleNum(ctc.getShouldHandleNum());
        ctCountFormat.setHandleNum(ctc.getHandleNum());
        ctCountFormat.setOnTimeHandleNum(ctc.getOnTimeHandleNum());
        ctCountFormat.setOutTimeHandleNum(ctc.getOutTimeHandleNum());
        ctCountFormat.setInTimeHandleNum(ctc.getInTimeHandleNum());
        ctCountFormat.setOutTimeNotHandleNum(ctc.getOutTimeNotHandleNum());
        ctCountFormat.setReWorkNum(ctc.getReWorkNum());
        ctCountFormat.setReWorkRate(ctc.getReWorkRate());
        ctCountFormat.setShouldClosedNum(ctc.getShouldClosedNum());
        ctCountFormat.setRealClosedNum(ctc.getRealClosedNum());
        ctCountFormat.setRealClosedRate(ctc.getRealClosedRate());
        ctCountFormat.setOnTimeClosedNum(ctc.getOnTimeClosedNum());
        ctCountFormat.setOnTimeClosedRate(ctc.getOnTimeClosedRate());
        ctCountFormat.setOverallMerit(ctc.getOverallMerit());
        ctCountFormat.setRank(ctc.getRank());
        return ctCountFormat;

    }

}
